﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Admin.UserControls
{
    public partial class PageControl : System.Web.UI.UserControl
    {
        private string pageName = "page"; //控件名称
        private string pageUrl = string.Empty; //Url地址
        protected bool showPage = true; //是否显示分页控件
        private int pageCount = 0; //总页数----属性不必要
        private int pageSize = 0; //每页记录数
        private int curPage = 0; //当前页码
        private int allCount = 0;//总记录数
        private int showCount = 0;//显示的页数

        //控件名称
        public string PageName
        {
            set
            {
                if (value.Trim().Length > 0)
                {
                    pageName = value.Trim();
                }
            }
        }
        //显示的页数
        public int ShowCount
        {
            set
            {
                showCount = value;
            }
            get
            {
                return showCount;
            }
        }
        //链接
        public string PageUrl
        {
            set
            {
                pageUrl = value;
            }
            get
            {
                return pageUrl;
            }
        }
        //总页数
        public int PageCount
        {
            set
            {
                pageCount = value;
            }
            get
            {
                return pageCount;
            }
        }
        //当前页码
        public int CurPage
        {
            set
            {
                curPage = value;
            }
            get
            {
                return curPage;
            }
        }
        //总数
        public int AllCount
        {
            set
            {
                allCount = value;
            }
            get
            {
                return allCount;
            }
        }
        //每页记录数
        public int PageSize
        {
            get
            {
                return pageSize;
            }
            set
            {
                pageSize = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //计算总页数
            if (pageSize > 0 && pageCount == 0)
            {
                if (allCount % pageSize > 0)
                {
                    pageCount = allCount / pageSize + 1;
                }
                else
                {
                    pageCount = allCount / pageSize;
                }
            }
            if (pageCount <= 1)
            {
                showPage = false;
            }

            if (!Page.IsPostBack)
            {
                //第一次加载时保存PageCount值
                ViewState["PageCount"] = pageCount;

                hlk_pre.Visible = false;
                hlk_first.Visible = false;
                hlk_next.Visible = false;
                hlk_last.Visible = false;

                if (curPage > 1 && curPage <= pageCount)
                {
                    hlk_pre.Visible = true;
                    hlk_first.Visible = true;

                    hlk_first.NavigateUrl = pageUrl.Replace(pageName + "=" + curPage, pageName + "=1");
                    hlk_pre.NavigateUrl = pageUrl.Replace(pageName + "=" + curPage, pageName + "=" + (curPage - 1));
                }
                if (curPage < pageCount)
                {
                    hlk_next.Visible = true;
                    hlk_last.Visible = true;

                    hlk_last.NavigateUrl = pageUrl.Replace(pageName + "=" + curPage, pageName + "=" + pageCount);
                    hlk_next.NavigateUrl = pageUrl.Replace(pageName + "=" + curPage, pageName + "=" + (curPage + 1));
                }
                DataTable dt = new DataTable();
                GetUrl(ref dt);

                rpt_page.DataSource = dt;
                rpt_page.DataBind();
                dt.Dispose();
            }
        }

        //获取分页数字形式的地址
        public void GetUrl(ref DataTable dt)
        {
            dt.Columns.Add("pageshow", typeof(string));

            if (showCount <= 0)
            {
                showCount = 3;
            }
            int temp = showCount / 2;

            if (temp >= curPage)
            {
                if (pageCount < showCount)
                {
                    for (int i = 1; i <= pageCount; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["pageshow"] = "<a href='" + pageUrl.Replace(pageName + "=" + curPage, pageName + "=" + i) + "'>" + i + "</a>";
                        if (i == curPage)
                        {
                            dr["pageshow"] = "<span class='current'>" + i + "</span>";
                        }
                        dt.Rows.Add(dr);
                    }
                }
                else
                {
                    for (int i = 1; i <= showCount; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["pageshow"] = "<a href='" + pageUrl.Replace(pageName + "=" + curPage, pageName + "=" + i) + "'>" + i + "</a>";
                        if (i == curPage)
                        {
                            dr["pageshow"] = "<span class='current'>" + i + "</span>";
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }
            else
            {
                if (curPage > pageCount - temp)
                {
                    if (pageCount >= showCount)
                    {
                        for (int i = pageCount - showCount + 1; i <= pageCount; i++)
                        {
                            DataRow dr = dt.NewRow();
                            dr["pageshow"] = "<a href='" + pageUrl.Replace(pageName + "=" + curPage, pageName + "=" + i) + "'>" + i + "</a>";
                            if (i == curPage)
                            {
                                dr["pageshow"] = "<span class='current'>" + i + "</span>";//class='page-cur'
                            }
                            dt.Rows.Add(dr);
                        }
                    }
                    else
                    {
                        for (int i = 1; i <= pageCount; i++)
                        {
                            DataRow dr = dt.NewRow();
                            dr["pageshow"] = "<a href='" + pageUrl.Replace(pageName + "=" + curPage, pageName + "=" + i) + "'>" + i + "</a>";
                            if (i == curPage)
                            {
                                dr["pageshow"] = "<span class='current'>" + i + "</span>";
                            }
                            dt.Rows.Add(dr);
                        }
                    }
                }
                else
                {
                    for (int i = curPage - temp; i <= curPage + temp; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["pageshow"] = "<a href='" + pageUrl.Replace(pageName + "=" + curPage, pageName + "=" + i) + "'>" + i + "</a>";
                        if (i == curPage)
                        {
                            dr["pageshow"] = "<span class='current'>" + i + "</span>";
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }
            this.ViewState["url"] = pageUrl;
        }

        //跳转到第X页
        protected void BtnPageSubmit_ServerClick(object sender, EventArgs e)
        {
            string url = this.ViewState["url"].ToString();
            int goPage = 1;
            int.TryParse(this.txt_GoPage.Value, out goPage);
            if (goPage < 1)
            {
                goPage = 1;
            }
            else
            {
                if (ViewState["PageCount"] != null)
                {
                    if (int.TryParse(ViewState["PageCount"].ToString(), out pageCount))
                    {
                        if (goPage > pageCount)
                        {
                            goPage = pageCount;
                        }
                    }
                }
            }
            Response.Redirect(url.Replace(pageName + "=" + curPage, pageName + "=" + goPage.ToString()), true);
        }
    }
}